package com.example.demo.repository;

import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    Optional<User> findByUsername(String username);

    Boolean existsByUsername(String username);

    Boolean existsByEmail(String email);

	// 分页过滤查询：按用户名/邮箱（模糊）与启用状态查询，任意条件可为空
	@Query("SELECT u FROM User u WHERE (:username IS NULL OR LOWER(u.username) LIKE LOWER(CONCAT('%', :username, '%'))) " +
			"AND (:email IS NULL OR LOWER(u.email) LIKE LOWER(CONCAT('%', :email, '%'))) " +
			"AND (:isActive IS NULL OR u.isActive = :isActive)")
	Page<User> findByFilters(
			@Param("username") String username,
			@Param("email") String email,
			@Param("isActive") Boolean isActive,
			Pageable pageable
	);
}
